#!/usr/bin/python
# -*-coding:utf-8-*-

#!/usr/bin/python
# -*-coding:utf-8-*-
# coding :utf-8
import os
import pandas as pd

try:
    try:
        from zg_factor_analysis_module.base.dir_info  import factor_lib_dir,save_dir,basic_data_dir,advance_dir,trade_factor_dir
    except:
        from .base.dir_info import factor_lib_dir, save_dir, basic_data_dir, advance_dir, trade_factor_dir
except:
    from base.dir_info import factor_lib_dir,save_dir,basic_data_dir,advance_dir,trade_factor_dir

class FactorAnalysisLib(object):
    def __init__(self,
                 factor_lib_dir=factor_lib_dir,
                 db='public',
                 basic_data_dir=basic_data_dir,
                 save_dir=save_dir,
                 advance_dir=advance_dir,
                 trade_factor_dir=trade_factor_dir):
        self.db = db
        self.factor_lib_dir = factor_lib_dir
        self.result_save_dir=save_dir
        self.basic_data_dir=basic_data_dir
        self.advance_dir=advance_dir
        self.trade_factor_dir=trade_factor_dir
        #self.df = pd.DataFrame()

    def read_factor_table(self, filename, filter_list=None, columns=[], start_row=None, end_row=None):
        if len(columns) == 0:
            df = pd.read_hdf(os.path.join(self.factor_lib_dir,self.db,'%s.h5' % (filename)),
                             key=filename,
                             mode='r',
                             start=start_row,
                             end=end_row,
                             where=filter_list)
        else:
            df = pd.read_hdf(os.path.join(self.factor_lib_dir, '%s.h5' % (filename)),
                             key=filename,
                             mode='r',
                             start=start_row,
                             end=end_row,
                             columns=columns,
                             where=filter_list)
        return df

    def read_trade_factor_table(self, filename, filter_list=None, columns=[], start_row=None, end_row=None):
        if len(columns) == 0:
            df = pd.read_hdf(os.path.join(self.trade_factor_dir,'%s.h5' % (filename)),
                             key=filename,
                             mode='r',
                             start=start_row,
                             end=end_row,
                             where=filter_list)
        else:
            df = pd.read_hdf(os.path.join(self.trade_factor_dir, '%s.h5' % (filename)),
                             key=filename,
                             mode='r',
                             start=start_row,
                             end=end_row,
                             columns=columns,
                             where=filter_list)
        return df

    def read_advance_table(self, filename, filter_list=None, columns=[], start_row=None, end_row=None):
        if len(columns) == 0:
            df = pd.read_hdf(os.path.join(self.advance_dir,'%s.h5' % (filename)),
                             key=filename,
                             mode='r',
                             start=start_row,
                             end=end_row,
                             where=filter_list)
        else:
            df = pd.read_hdf(os.path.join(self.advance_dir, '%s.h5' % (filename)),
                             key=filename,
                             mode='r',
                             start=start_row,
                             end=end_row,
                             columns=columns,
                             where=filter_list)
        return df

    def read_basic_data_table(self, filename, filter_list=None, columns=[], start_row=None, end_row=None):
        if len(columns) == 0:
            df = pd.read_hdf(os.path.join(self.basic_data_dir, '%s.h5' % (filename)),
                             key=filename,
                             mode='r',
                             start=start_row,
                             end=end_row,
                             where=filter_list)
        else:
            df = pd.read_hdf(os.path.join(self.basic_data_dir, '%s.h5' % (filename)),
                             key=filename,
                             mode='r',
                             start=start_row,
                             end=end_row,
                             columns=columns,
                             where=filter_list)
        return df

    def show_factor_library_db(self):
        '''显示FACTOR LIBRARY的文件夹内的文件信息'''
        return list(os.walk(os.path.join(self.factor_lib_dir,self.db)))[0][-1]

    def show_save_library_db(self):
        '''显示输出文件夹的文件信息'''
        return list(os.walk(self.result_save_dir))[0][-1]

    def show_basic_library_db(self):
        '''显示源数据的文件夹内的文件信息'''
        return list(os.walk(self.basic_data_dir))[0][-1]

    def show_advance_library_db(self):
        '''显示源数据的文件夹内的文件信息'''
        return list(os.walk(self.advance_dir))[0][-1]

    def show_trade_library_db(self):
        '''显示源数据的文件夹内的文件信息'''
        return list(os.walk(self.trade_factor_dir))[0][-1]

if __name__ == '__main__':
    F=FactorAnalysisLib()

    #显示文件夹文件列表
    #F.show_factor_library_db()
    #F.show_save_library_db()
    print(F.show_trade_library_db())
    #factor_data=F.read_factor_table('valuation_bp')
    #new_test=F.read_advance_table('style_earn_factor_value')